<template>
  <div class="wrap">
    <div class="wrap-con">
      <div class="header">
        <div class="header-img">
          <img
            src="https://api.java.crmeb.net/crmebimage/public/operation/2021/09/23/aa6e46fd9fbd424e8cb636a34db50176nj0e4r0f6o.png"
            alt=""
          />
        </div>
        <!-- 搜索框 -->
        <div class="header-search" @click="to">
          <van-search
            v-model="value"
            shape="round"
            placeholder="请输入搜索关键词"
          />
        </div>
      </div>
      <!-- 轮播 -->
      <div class="swiper">
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item v-for="item in list.banner" :key="item.id">
            <img :src="item.pic" alt="" />
          </van-swipe-item>
        </van-swipe>
      </div>
      <!-- 百货 -->
      <div class="notice">
        <div class="notice-img" @click="toNews">
          <img
            src="https://api.java.crmeb.net/crmebimage/change/new_header/new_header5.png"
            alt=""
          />
        </div>
        <div class="line">
          <span>|</span>
        </div>
        <div class="notice-text" @click="toMerch">
          <p>百货</p>
        </div>
        <div class="sign">
          <p>></p>
        </div>
      </div>
      <!-- 九宫格 -->
      <div class="display-item">
        <van-grid :column-num="5" :border="false">
          <van-grid-item
            v-for="item in list.menus"
            :key="item.id"
            :icon="item.pic"
            :text="item.name"
          />
        </van-grid>
      </div>

      <!-- 限时秒杀 -->
      <div class="limited-time-kill" v-if="secKillResponse">
        <div class="limited-timer">
          <img src="@/assets/images/seckillTitle.png" alt="" />
          <div class="timer-top">
            <span class="once"
              >{{ secKillResponse.time.split(",")[0] }} 场</span
            >
            &nbsp;&nbsp;&nbsp;&nbsp;<span>{{ secKillTimer }}</span>
          </div>
          <div class="go">GO ></div>
        </div>
        <div class="time-content">
          <div
            class="time-content-text"
            v-for="pic in productList"
            :key="pic.id"
            @click="$router.push(`/DetailsAll/${pic.id}?type=seckill`)"
          >
            <img :src="pic.image" alt="" />
            <div class="con-text">
              <p>{{ pic.title }}</p>
              <h4>￥{{ pic.price }}</h4>
              <span>￥{{ pic.otPrice }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 领取优惠劵 -->
      <div class="coupon">
        <div class="coupon-tit">
          <h3>领取优惠券</h3>
          <span @click="toCoupons">查看更多></span>
        </div>
        <!-- 优惠券列表 -->
        <div class="scroll-view">
          <!-- 优惠券，满可用 -->
          <div class="scorll-view-item" v-for="item in coupons" :key="item.id">
            <div
              class="item-right"
              :style="{ backgroundColor: item.isUse ? '#ccc' : '#ff63a9' }"
            >
              <div class="follow" v-if="item.isUse">
                <i>已领取</i>
              </div>
              <div class="follow" v-else @click="getCoupons(item.id)">
                <i>领取</i>
              </div>
              <div
                class="item-left"
                :style="{
                  backgroundColor: item.isUse ? '#f6f6f6' : '#ffd8e7',
                }"
              >
                <div class="item-flex">
                  <span
                    class="money"
                    :style="{
                      color: item.isUse ? '#fff' : '#fff',
                      backgroundColor: item.isUse ? '#ccc' : '#ff63a9',
                    }"
                  >
                    ￥
                  </span>
                  <span
                    class="number"
                    :style="{ color: item.isUse ? '#ccc' : '#ff448f' }"
                    >{{ parseInt(item.money) }}</span
                  >
                </div>
                <span
                  class="sum"
                  :style="{
                    color: item.isUse ? '#ccc' : '#ff63a9',
                  }"
                  >满{{ parseInt(item.minPrice) }}可用</span
                >
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 拼团惠 -->
      <div class="discount">
        <div class="discount-content">
          <div class="discount-avatar">
            <div
              class="discount-content-top"
              v-for="item in discount.avatarList"
              :key="item.id"
            >
              <img :src="item" alt="" />
            </div>
            <span>{{ discount.totalPeople }}人参与</span>
          </div>
          <div class="discount-img">GO ></div>
        </div>
        <div class="discount-display">
          <div class="cc">
            <div
              class="display-container"
              v-for="item in discount.productList"
              :key="item.id"
              @click="$router.push(`/DetailsAll/${item.id}?type=pt`)"
            >
              <img :src="item.image" alt="" />
              <p>{{ item.title }}</p>
              <h3>￥{{ item.price }}</h3>
              <span>￥{{ item.otPrice }}</span>
            </div>
          </div>
        </div>
      </div>
      <!-- 商品排行榜 -->
      <div class="product-ranking">
        <div class="rank-title">
          <div class="rank-title-list">
            <p><van-icon name="good-job-o" color="white" size="16" /></p>
            <h3>商品排行榜</h3>
          </div>
          <p>更多 ></p>
        </div>
        <div
          class="product-ranking-list"
          v-for="item in goods"
          :key="item.id"
          @click="$router.push(`/DetailsAll/${item.id}?type=normal`)"
        >
          <div class="ranking-list-img">
            <img :src="item.image" alt="item" />
          </div>
          <div class="ranking-list-text"> x
            <h4>{{ item.storeName }}</h4>
            <div class="list-bottom">
              <p>
                <em>￥</em>
                <b>{{ item.price }}</b>
                <span>销量 {{ item.sales }}件</span>
              </p>
              <p class="round">
                <van-icon name="cart-o" color="white" size="16" />
              </p>
            </div>
          </div>
        </div>
      </div>
      <!-- 精品推荐 -->
      <div class="recomment">
        <van-sticky>
          <div class="recomment-list">
            <div
              class="recomment-con"
              v-for="item in recommend.explosiveMoney"
              :key="item.id"
            >
              <div class="click" @click="showId(item.type)">
                <h3 :class="{ active: type == item.type }">
                  {{ item.name }}
                </h3>
                <p :class="{ activeTitle: type == item.type }">
                  {{ item.info }}
                </p>
              </div>
            </div>
          </div>
        </van-sticky>
        <!-- 普通产品 -->
        <div class="loading">
          <van-loading size="20" type="spinner" vertical v-show="isShow" />
        </div>
        <div class="product-wrap">
          <div
            class="product-wrap-list"
            v-for="item in product"
            :key="item.id"
            @click="$router.push(`/DetailsAll/${item.id}?type=normal`)"
          >
            <img :src="item.image" alt="" />
            <div class="pro-text">
              <h3>{{ item.storeName }}</h3>
              <span>￥{{ item.otPrice }}</span>
              <p>￥{{ item.price }}</p>
            </div>
          </div>
        </div>
      </div>
      <!-- 我是有底线的 -->
      <div class="isbot">
        <p>我是有底线的</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "IndexVue",
  data() {
    return {
      value: "", // 搜索框
      list: [], // 轮播图
      secKillResponse: null, //限时秒杀
      timer: "", //限时秒杀
      productList: [], //限时秒杀
      secKillTimer: "00:00:00", //限时秒杀
      coupons: null, // 领取优惠卷
      discount: [],
      goods: [], // 商品排行榜
      recommend: [], // 底部精品推荐导航
      product: [],
      type: 1, // 默认高亮第一个
      isShow: false, // 加载。。。
    };
  },
  methods: {
    calcSecKillTimer() {
      // 截至时间的时间戳
      let destTimer = this.secKillResponse.timeSwap * 1;
      // 当前时间的时间戳
      let curTimer = parseInt(Date.now() / 1000);
      // 剩余总秒数
      let totalSec = destTimer - curTimer;
      // 转化时分秒
      let second = parseInt(totalSec % 60);
      let minute = parseInt((totalSec / 60) % 60);
      let hour = parseInt((totalSec / 60 / 60) % 24);
      // 补零
      second = second < 10 ? "0" + second : second;
      minute = minute < 10 ? "0" + minute : minute;
      hour = hour < 10 ? "0" + hour : hour;
      this.secKillTimer = `${hour}:${minute}:${second}`;
    },
    // 底部商品信息
    showId(id) {
      this.type = id;
      this.isShow = true;
      this.$request(
        `https:/cremb/index/product/${this.type}?page=1&limit=10`
      ).then((data) => {
        this.product = data.data.list;
        this.isShow = false;
      });
    },
    // 首页搜索跳转
    to() {
      this.$router.push("/SearchInfo");
    },
    // 首页新闻简报跳转
    toNews() {
      this.$router.push({
        name: "NewsInfo",
      });
    },
    // 首页百货跳转
    toMerch() {
      this.$router.push({
        name: "MerchInfo",
      });
    },
    // 优惠卷跳转
    toCoupons() {
      this.$router.push({
        name: "CouponsList",
      });
    },
    // 领取优惠卷
    getCoupons(id) {
      this.$requestTools("/crmeb/coupon/receive", "POST", {
        couponId: id,
      }).then(() => {
        this.$requestTools("/crmeb/coupons?page:1&limit:2").then(({ data }) => {
          this.coupons = data.data;
          console.log(this.coupons);
        });
      });
    },
  },

  created() {
    // 轮播图
    this.$request("/crmeb/index").then((data) => {
      this.list = data.data;
      // console.log(this.list);
    });
    //限时秒杀
    this.$request("/crmeb/seckill/index").then((data) => {
      if (data) {
        this.secKillResponse = data.data.secKillResponse;
        this.calcSecKillTimer();
        this.productList = data.data.productList;
        // console.log(this.productList);
      }
      if (data) {
        this.timer = setInterval(() => {
          this.calcSecKillTimer();
        }, 1000);
      }
    });

    // 获取优惠卷列表
    // https://apif.java.crmeb.net/api/front/coupons?page=1&limit=20
    this.$requestTools("/crmeb/coupons?page=1&limit=20").then((data) => {
      this.coupons = data.data.data;
      console.log(this.coupons);
    });

    // 拼团惠
    this.$request("/crmeb/combination/index").then((data) => {
      this.discount = data.data;
      // console.log(this.discount);
    });
    //商品排行榜
    this.$request("/crmeb/product/leaderboard").then((data) => {
      this.goods = data.data.slice(0, 3);
      // console.log(this.goods);
    });
    // 精品推荐的导航
    this.$request("/crmeb/index").then((data) => {
      this.recommend = data.data;
      // console.log(this.recommend);
    });
    // 精品推荐产品的内容
    this.$request("/crmeb/index/product/1?page=1&limit=10").then((data) => {
      this.product = data.data.list;
      // console.log(this.product);
    });
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
.wrap {
  height: calc(100vh - 50px);
  overflow-y: scroll;
  &::-webkit-scrollbar {
    display: none;
  }
  .wrap-con {
    .header {
      padding: 15px;
      box-sizing: border-box;
      height: 54px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #ff448f !important;
      img {
        width: 58px;
        height: 20px;
      }
      ::v-deep .van-search {
        width: 254px;
        height: 30px;
        border-radius: 18px;
        overflow: hidden;
        padding: 0 10px;
      }
      ::deep .van-cell__value {
        margin-top: 20px;
      }
    }
    /* 轮播 */
    .swiper {
      padding: 15px;
      box-sizing: border-box;
      background: -webkit-linear-gradient(270deg, #ff448f, #f5f5f5);
      .my-swipe .van-swipe-item {
        // color: #fff;
        // font-size: 20px;
        // line-height: 159px;
        // text-align: center;
        // background-color: #39a9ed;
        img {
          width: 100%;
          height: 159px;
          border-radius: 10px;
        }
      }
    }
    /* 百货 */
    .notice {
      width: 92%;
      margin: 0 auto;
      margin-top: 10px;
      background-color: white;
      display: flex;
      justify-content: space-around;
      align-items: center;
      color: #ccc;
      .notice-img {
        padding: 10px 0;
        img {
          width: 100%;
          height: 17px;
          display: block;
        }
      }
      .notice-text {
        width: 60%;
        font-size: 12px;
      }
    }
    /* 九宫格 */
    .display-item {
      padding: 15px;
      .van-grid {
        border-radius: 10px;
        overflow: hidden;
      }
    }
    /* 领取优惠卷 */
    .coupon {
      background-color: white;
      border-radius: 8px;
      width: 343px;
      margin: 0 16px;
      overflow: hidden;
      margin-bottom: 18px;
      margin-top: 20px;
      .coupon-tit {
        display: flex;
        justify-content: space-between;
        padding: 10px;
        box-sizing: border-box;
        h3 {
          font-size: 16px;
          line-height: 24px;
          color: #333;
        }
        span {
          font-size: 13px;
          font-weight: 400;
          line-height: 24px;
          color: #999;
        }
      }
      .scroll-view {
        display: flex;
        padding: 10px;
        width: calc(100vw - 40px);
        overflow-x: scroll;
        &::-webkit-scrollbar {
          display: none;
        }
        .scorll-view-item {
          margin-right: 10px;
          .item-right {
            background: #ff63a9;
            width: 124px;
            height: 59px;
            border-radius: 7px;
            // 领取按钮
            .follow {
              i {
                float: right;
                color: white;
                display: block;
                width: 20px;
                margin-top: 10px;
                font-size: 12px;
                margin-right: 3px;
                font-style: normal;
              }
            }
          }
          .item-left {
            background-color: #ffd8e7;
            width: 94px;
            height: 59px;
            border-radius: 7px;
            position: relative;
            text-align: center;
            box-sizing: border-box;
            padding: 10px 0;
            z-index: 1000;
            &::before {
              content: "";
              position: absolute;
              width: 12px;
              height: 12px;
              top: 50%;
              transform: translateY(-50%);
              left: -5px;
              background: white;
              border-radius: 50%;
            }
            .item-flex {
              display: flex;
              align-items: center;
              justify-content: center;
              .money {
                background: #ff448f;
                border-radius: 50%;
                color: white;
                text-align: center;
                font-size: 12px;
                display: block;
                width: 14px;
                height: 14px;
                margin-right: 2px;
                text-align: center;
              }
              .number {
                font-size: 24px;
                line-height: 24px;
                font-weight: 600;
                color: #ff448f;
                text-align: center;
              }
            }
            .sum {
              color: #ff448f;
              font-size: 13px;
              text-align: left;
            }
          }
        }
      }
    }
    /* 拼团惠 */
    .discount {
      background-image: url("@/assets/images/index.png");
      background-size: 100%;
      background-repeat: no-repeat;
      width: 343px;
      background-color: #fff;
      border-radius: 6px;
      margin: 20px auto 14px auto;
      padding: 10px 11px 11px 11px;
      box-sizing: border-box;
      .discount-content {
        display: flex;
        justify-content: space-around;
        .discount-avatar {
          display: flex;
          margin-left: 65px;
          .discount-content-top {
            img {
              width: 18px;
              height: 18px;
              border-radius: 50%;
              margin-left: -3px;
            }
          }
          span {
            margin-left: 8px;
            color: #ff448f;
            font-size: 12px;
          }
        }

        .discount-img {
          width: 42px;
          height: 19px;
          background: linear-gradient(142deg, #ffe9ce, #ffd6a7);
          opacity: 1;
          border-radius: 18px;
          font-size: 10px;
          color: #fe960f;
          padding-left: 3px;
          font-weight: 800;
          line-height: 19px;
          text-align: center;
          margin-right: -33px;
        }
      }
      .discount-display {
        margin-top: 10px;
        .cc {
          overflow-y: scroll;
          display: flex;
          background-color: white;
          box-sizing: border-box;
          padding-bottom: 15px;
          &::-webkit-scrollbar {
            display: none;
          }
          .display-container {
            width: 119px;
            flex-shrink: 0;
            img {
              width: 109px;
              height: 109px;
              border-radius: 5px;
            }
            p {
              width: 109px;
              font-size: 14px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            h3 {
              color: #ff448f;
              font-size: 17px;
              font-weight: 700;
            }
            span {
              font-size: 12px;
              color: #999;
              text-decoration: line-through;
            }
          }
        }
      }
    }
    /* 限时秒杀 */
    .limited-time-kill {
      box-sizing: border-box;
      width: 92%;
      padding: 10px 15px;
      background: -webkit-linear-gradient(
        270deg,
        #ff448f,
        hsla(0, 0%, 100%, 0.2) 72%,
        #fff
      );
      margin: 0px auto;
      border-radius: 10px;
      .limited-timer {
        display: flex;
        justify-content: space-between;
        justify-items: center;
        img {
          width: 73px;
          height: 19px;
        }
        .timer-top {
          .once {
            color: #fff;
            font-weight: bold;
          }
          span {
            // color: #ff448f;
            color: #fff;
          }
        }
        .go {
          width: 42px;
          height: 19px;
          background: linear-gradient(142deg, #ffe9ce, #ffd6a7);
          opacity: 1;
          border-radius: 18px;
          font-size: 10px;
          color: #fe960f;
          padding-left: 3px;
          font-weight: 800;
          line-height: 19px;
          text-align: center;
        }
      }
      .time-content {
        display: flex;
        align-items: center;
        overflow-y: scroll;
        margin-top: 15px;
        &::-webkit-scrollbar {
          display: none;
        }
        .time-content-text {
          flex-shrink: 0;
          margin-right: 10px;
          margin-bottom: 10px;
          img {
            width: 92px;
            height: 92px;
            border-radius: 5px;
          }
          .con-text {
            width: 92px;
            p {
              font-size: 12px;
              word-break: break-all;
              display: -webkit-box;
              -webkit-line-clamp: 1;
              -webkit-box-orient: vertical;
              overflow: hidden;
            }
            h4 {
              font-size: 15px;
              color: #ff448f;
            }
            span {
              font-size: 14px;
              color: #999;
              text-decoration: line-through;
            }
          }
        }
      }
    }
    /* 商品排行榜 */
    .product-ranking {
      margin-top: 15px;
      .rank-title {
        background-color: #fff;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px;
        .rank-title-list {
          display: flex;
          p {
            width: 22px;
            height: 22px;
            background-color: #ff448f;
            border-radius: 50%;
            text-align: center;
            line-height: 22px;
          }
          h3 {
            font-size: 18px;
            margin-left: 5px;
          }
        }
        p {
          font-size: 2.933vw;
          font-weight: 400;
          line-height: 6px;
          color: #999;
        }
      }
      padding: 0 15px;
      .product-ranking-list {
        display: flex;
        justify-content: space-between;
        padding: 10px;
        background: white;
        .ranking-list-img {
          img {
            width: 89px;
            height: 89px;
            border-radius: 5px;
          }
        }
        .ranking-list-text {
          width: 80%;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          margin-left: 15px;
          padding: 4px;
          border-bottom: 1px solid #ccc;
          h4 {
            font-size: 14px;
            font-weight: 400;
            color: #333;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }
          .list-bottom {
            display: flex;
            justify-content: space-between;
            p {
              em {
                font-size: 12px;
                font-style: normal;
                color: #ff448f;
              }
              b {
                font-size: 18px;
                color: #ff448f;
              }
              span {
                font-size: 14px;
                color: #ccc;
                margin-left: 5px;
              }
            }
            .round {
              width: 23px;
              height: 23px;
              border-radius: 50%;
              background-color: #ff448f;
              text-align: center;
              line-height: 23px;
            }
          }
        }
      }
    }
    /* 精品推荐 */
    .recomment {
      padding: 0 10px;
      margin-top: 15px;
      .recomment-list {
        display: flex;
        justify-content: space-around;
        align-items: center;
        background-color: #eee;
        .recomment-con {
          .click {
            h3 {
              font-size: 18px;
              font-weight: normal;
            }
            p {
              text-align: center;
              font-size: 12px;
              color: gray;
            }
          }
        }
      }
      .active {
        color: #ff448f;
      }
      .activeTitle {
        background: linear-gradient(90deg, #ff448f, #ff67ad);
        border-radius: 16px;
        color: white !important;
      }
      .product-wrap {
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        .product-wrap-list {
          width: 48%;
          margin-top: 10px;
          img {
            width: 167px;
            height: 165px;
            border-radius: 5px 5px 0 0;
            display: block;
          }
          .pro-text {
            width: 98%;
            background-color: #fff;
            padding: 10px;
            box-sizing: border-box;
            h3 {
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 1;
              overflow: hidden;
              font-size: 14px;
              font-weight: normal;
            }
            span {
              font-size: 12px;
              color: gray;
            }
            p {
              color: #ff448f;
            }
          }
        }
      }
    }
    /* 我是有底线的 */
    .isbot {
      p {
        text-align: center;
        color: gray;
        font-size: 12px;
        padding: 15px;
      }
    }
  }
}
</style>
